package com.jzr.common.entity.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.jzr.common.exception.BaseException;
import com.jzr.common.exception.ErrorType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.time.Instant;
import java.time.ZonedDateTime;

@ApiModel(description = "rest请求的返回模型，所有rest正常都返回该类的对象")
@Getter
public class Result<T> {

    public static final int SUCCESSFUL_CODE = 0;
    public static final String SUCCESSFUL_MESG = "处理成功";

    @ApiModelProperty(value = "处理结果code", required = true)
    protected int code;
    @ApiModelProperty(value = "处理结果status，和code相同")
    protected int status;
    @ApiModelProperty(value = "处理结果描述信息")
    protected String message;
    @ApiModelProperty(value = "处理结果描述信息，和message相同")
    protected String msg;
    @ApiModelProperty(value = "数据总行数")
    protected long count;
    @ApiModelProperty(value = "请求结果生成时间戳")
    protected Instant timestamp;
    @ApiModelProperty(value = "处理结果数据信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)

    protected T data;

    public Result() {
        this.timestamp = ZonedDateTime.now().toInstant();
    }

    /**
     * @param errorType
     */
    public Result(ErrorType errorType) {
        this.status = errorType.getCode();
        this.code = errorType.getCode();
        this.message = errorType.getMsg();
        this.msg = errorType.getMsg();
        this.timestamp = ZonedDateTime.now().toInstant();
    }

    /**
     * @param errorType
     * @param data
     */
    public Result(ErrorType errorType, T data) {
        this(errorType);
        this.data = data;
    }

    /**
     * 内部使用，用于构造成功的结果
     *
     * @param code
     * @param message
     * @param data
     */
    protected Result(int code, String message, T data) {
        this.code = code;
        this.status = code;
        this.message = message;
        this.msg = message;
        this.data = data;
        this.timestamp = ZonedDateTime.now().toInstant();
    }
    protected Result(int code, String message, T data, int count) {
        this.code = code;
        this.status = code;
        this.message = message;
        this.msg = message;
        this.data = data;
        this.count = count;
        this.timestamp = ZonedDateTime.now().toInstant();
    }

    /**
     * 快速创建成功结果并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESSFUL_CODE, SUCCESSFUL_MESG, data);
    }

    /**
     * 有 msg 说明的成功. 不返回数据, 当返回 "添加成功" 这一类时
     */
    public static Result success(String msg) {
        return new Result<>(SUCCESSFUL_CODE, msg, null);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(SUCCESSFUL_CODE, msg, data);
    }

    public static <T> Result<T> success(String msg, T data, int count) {
        return new Result<>(SUCCESSFUL_CODE, msg, data, count);
    }

    /**
     * 快速创建成功结果
     *
     * @return Result
     */
    public static Result success() {
        return success(null);
    }

    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result fail() {
        return new Result(ErrorType.SYSTEM_ERROR);
    }

    /**
     * 系统异常类没有返回数据
     *
     * @param baseException
     * @return Result
     */
    public static Result fail(BaseException baseException) {
        return fail(baseException.getErrorType(), null);
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static <T> Result<T> fail(BaseException baseException, T data) {
        return new Result<>(baseException.getErrorType(), data);
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param errorType
     * @param data
     * @return Result
     */
    public static <T> Result<T> fail(ErrorType errorType, T data) {
        return new Result<>(errorType, data);
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param errorType
     * @return Result
     */
    public static Result fail(ErrorType errorType) {
        return Result.fail(errorType, null);
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static <T> Result<T> fail(T data) {
        return new Result<>(ErrorType.SYSTEM_ERROR, data);
    }

    public static Result fail(String errorMsg) {
        return new Result<>(ErrorType.SYSTEM_ERROR.getCode(), errorMsg,null);
    }


    /**
     * 成功code=000000
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isSuccess() {
        return SUCCESSFUL_CODE == this.code;
    }

    /**
     * 失败
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isFail() {
        return !isSuccess();
    }

    public String getMsg(){
        return message;
    }

    public int getStatus(){return code;}
}
